﻿using System;
using System.ComponentModel;
using Model01.Exceptions;

using DrawingSize = System.Drawing.Size;

namespace Model01.Objects
{
    /// <summary>
    /// Stores an ordered pair of integers, typically width and height of an object.
    /// </summary>
    [Serializable]
    public class Size
    {
        #region Fields

        private int width = 1;
        private int height = 1;

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="width">The width component of Size.</param>
        /// <param name="height">The height component of Size.</param>
        public Size(int width, int height)
        {
            Width = width;
            Height = height;
        }

        #region Properties

        /// <summary>
        /// Gets or sets the width component of Size.
        /// </summary>
        /// <exception cref="Model01.Exceptions.NonPositiveWidthException"/>
        public int Width
        {
            get { return width; }
            set
            {
                if (value > 0)
                    width = value;
                else
                    throw new NonPositiveWidthException();
            }
        }

        /// <summary>
        /// Gets or sets the height component of Size.
        /// </summary>
        /// <exception cref="Model01.Exceptions.NonPositiveHeightException"/>
        public int Height
        {
            get { return height; }
            set
            {
                if (value > 0)
                    height = value;
                else
                    throw new NonPositiveHeightException();
            }
        }

        #endregion

        #region Implicit operators

        /// <summary>
        /// User-defined conversion from Model01.Objects.Size to System.Drawing.Size.
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static implicit operator DrawingSize(Size size)
        {
            return new DrawingSize(size.Width, size.Height);
        }

        /// <summary>
        /// User-defined conversion from System.Drawing.Size to Model01.Objects.Size.
        /// </summary>
        /// <param name="drawSize"></param>
        /// <returns></returns>
        public static implicit operator Size(DrawingSize drawingSize)
        {
            return new Size(drawingSize.Width, drawingSize.Height);
        } 

        #endregion
    }
}
